shell bash终端中输出的颜色和格式详解(超详细) |
您所在的位置:网站首页 › shell encodedUrl › shell bash终端中输出的颜色和格式详解(超详细) |
文章目录1) 格式1.1 Set1.2 Reset2)8/16 Colors2.1 前景(文字)2.2 背景3)88/256颜色3.1 前景(文字)3.2 背景色4)组合属性5)终端兼容性6)示例程序6.1 Colors and formatting (16 colors)6.2 256 colors参考
ANSI/VT100终端和终端仿真器不只是能够显示黑色和白色文本; 由于转义序列,它们可以显示颜色和格式化文本。这些序列由Escape字符(通常用“^[” 或""表示)组成,后跟一些其他字符:“[FormatCodem”。 在Bash中,可以使用以下语法获取字符: \e \033 \x1B 例子: 代码(Bash) Preview echo -e “\ e [31mHello World \ e [0m”![]() ![]() 注意: 该命令的-e选项echo启用转义序列的解析。 "\e[0m"序列删除所有属性(格式和颜色)。在每个彩色文本的末尾添加它是个好主意。;) 本页中的示例使用Bash,但ANSI/VT100转义序列可用于各种编程语言。 1) 格式 1.1 Set code Description Example Preview 1 粗体/高亮 echo -e “Normal \ e [1mBold”![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 以下颜色适用于大多数终端和终端仿真器2), 请参阅兼容性列表以获取更多信息。 颜色可能因终端配置而异。 Code Color Example Preview 39 默认前景色 echo -e “Default \ e [39mDefault”![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 某些终端(参见兼容性列表)可以支持88或256种颜色。以下是允许您使用它们的控制序列。 注意:颜色编号256仅由vte支持(GNOME终端,XFCE4终端,Nautilus终端,终结者…)。 注意2:88色终端(如rxvt)与256色终端的颜色图不同。要显示88色终端颜色映射,请在88色终端中运行“ 256-colors.sh ”脚本。 3.1 前景(文字)要使用前景中的256种颜色之一(文本颜色),控制序列为“ [38;5;ColorNumberm ”,其中ColorNumber是以下颜色之一:
要在背景上使用256种颜色中的一种,控制序列为“ [48;5;ColorNumberm ”,其中ColorNumber是以下颜色之一:
终端允许属性组合。属性必须用分号(“ ;”)分隔。 Description Code (Bash) Preview Bold + Underlined echo -e “\e[1;4mBold and Underlined”![]() ![]() 表中使用的符号: “ ok”:终端支持。 “ ~”:终端以特殊方式支持。 “ -”:终端根本不支持。 6)示例程序 6.1 Colors and formatting (16 colors)以下shell脚本显示了许多可能的属性组合(但不是全部,因为它一次只使用一个格式属性)。 #!/bin/bash # This program is free software. It comes without any warranty, to # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. #Background for clbg in {40..47} {100..107} 49 ; do #Foreground for clfg in {30..37} {90..97} 39 ; do #Formatting for attr in 0 1 2 4 5 7 ; do #Print the result echo -en "\e[${attr};${clbg};${clfg}m ^[${attr};${clbg};${clfg}m \e[0m" done echo #Newline done done exit 0以下脚本显示某些终端和终端仿真器(如XTerm和GNOME Terminal)上可用的256种颜色。 #!/bin/bash # This program is free software. It comes without any warranty, to # the extent permitted by applicable law. You can redistribute it # and/or modify it under the terms of the Do What The Fuck You Want # To Public License, Version 2, as published by Sam Hocevar. See # http://sam.zoy.org/wtfpl/COPYING for more details. for fgbg in 38 48 ; do # Foreground / Background for color in {0..255} ; do # Colors # Display the color printf "\e[${fgbg};5;%sm %3s \e[0m" $color $color # Display 6 colors per lines if [ $((($color + 1) % 6)) == 4 ] ; then echo # New line fi done echo # New line done exit 0Linux console codes manual (’‘man console_codes’’) XTerm Control Sequences |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |